package com.puzzlebazar.client.util;


public abstract class ObserverTool extends Tool implements Observer {

	private final Observable observable;

	private boolean internalEnabled = true;
	private boolean externalEnabled = true;
	
	/**
	 * Creates a new observer tool identified by the passed text.
	 * 
	 * @param text The text to write on the button
	 */
	public ObserverTool( String text, Observable observable ) {
		super( text );

		this.observable = observable;
		observable.addObserver( this );	
	}
	
	/**
	 * Indicates wether the internal condition allows enabling this tool
	 * 
	 * @param enabled True to enable the tool internally, false to disable it
	 */
	protected void setInternalEnabled( boolean enabled ) {
		internalEnabled = enabled;
		checkEnabled();
	}

	@Override
	public void setEnabled( boolean enabled ) {
		externalEnabled = enabled;		
		checkEnabled();
	}

	/**
	 * Checks if the tool is supposed to be enabled
	 */
	private void checkEnabled() {
		super.setEnabled( internalEnabled && externalEnabled );
	}
	
	@Override
	public void release() {
		observable.deleteObserver( this );
	}
}
